// pages/insurance/orderDetail/index.js
import request from '../../../baseRequest'
import urls from '../../../requestUrl'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderInfo: {},
        accidentInfo: {},
        actionBtns: [{
                text: '取消订单',
                bgColor: '#f90',
                id: 1
            },
            {
                text: '立即支付',
                bgColor: '#19be6b',
                id: 2
            }
        ],
        actionBtns2: [{
                text: '事故上报',
                bgColor: '#2d8cf0',
                id: 1
            },
            {
                text: '安全结案',
                bgColor: '#ed3f14',
                id: 2
            }
        ],
        actionBtns3: [{
                text: '刷新进度',
                bgColor: '#19be6b',
                id: 1
            },
            {
                text: '修改事故信息',
                bgColor: '#ed3f14',
                id: 2
            }
        ],
        actionBtns4: [{
            text: '刷新进度',
            bgColor: '#19be6b',
            id: 1
        }]
    },

    /**获取服务订单详情 */
    getInsuranceOrderDetail() {
        let url = `${urls.insurance.detailOrder}/${this.data.orderId}`
        request.getRequest(url, {}, (res) => {
            this.setData({
                orderInfo: res.orderInfo,
                accidentInfo: res.accidentInfo || null,
                userCarImgs: res.userCarImgs,
                carBackSideImgs: res.carBackSideImgs,
                carFrontSideImgs: res.carFrontSideImgs,
                carNumberImgs: res.carNumberImgs || [],
                userLicenceImgs: res.userLicenceImgs || []
            })
        }, () => {})
    },

    /**安全结案订单 */
    handelCompleteOrder() {
        let param = { orderId: this.data.orderId }
        request.postRequest(urls.insurance.completeOder, param, (res) => {
            wx.showModal({
                title: '温馨提示',
                content: '服务订单已成功结案',
                showCancel: false,
                success: res => {
                    wx.navigateBack()
                }
            })
        }, () => {})
    },

    /**取消订单 */
    handelCancelOrder() {
        wx.showModal({
            title: '温馨提示',
            content: '是否确定取消本次服务订单？',
            confirmText: '确定取消',
            success: res => {
                if (res.confirm) {
                    let param = { orderId: this.data.orderId }
                    request.postRequest(urls.insurance.cancelOrder, param, (res) => {
                        wx.showModal({
                            title: '温馨提示',
                            content: '服务订单已成功取消',
                            showCancel: false,
                            success: res => {
                                wx.navigateBack()
                            }
                        })
                    }, () => {})
                }
            }
        })

    },

    /**刷新事故进度 */
    handelRefreshAccidentInfo() {
        let url = `${urls.accident.accidentRefresh}/${this.data.accidentInfo.accidentId}`
        request.getRequest(url, {}, (res) => {
            this.setData({
                ['accidentInfo.accidentStatus']: res.accidentInfo.accidentStatus
            })
        }, () => {})
    },

    /**处理按钮点击事件 */
    handelBtnClick1(e) {
        if (e.detail.id === '1') {
            //取消订单
            // wx.showToast({
            //   title: '暂时未开放',
            //   icon: 'none'
            // })
            this.handelCancelOrder()
        } else {
            wx.navigateTo({
                url: `/pages/insurance/orderPay/index?id=${this.data.orderId}&type=2`,
            })
        }
    },
    handelBtnClick2(e) {
        if (e.detail.id === '1') {
            //事故上报
            wx.navigateTo({
                url: `/pages/accidentReport/reportCreate/index?orderid=${this.data.orderId}`,
            })
        } else {
            //安全结案
            wx.showModal({
                title: '温馨提醒',
                content: '是否确认继续“安全结案”操作？',
                confirmText: '确认结案',
                success: res => {
                    if (res.confirm) {
                        this.handelCompleteOrder()
                    }
                }
            })
        }
    },
    handelBtnClick3(e) {
        if (e.detail.id === '1') {
            //刷新进度
            // wx.showToast({
            //   title: '暂时未开放',
            //   icon: 'none'
            // })
            this.handelRefreshAccidentInfo()
        } else {
            //修改事故信息
            wx.navigateTo({
                url: `/pages/accidentReport/reportEdit/index?id=${this.data.accidentInfo.accidentId}`,
            })
        }
    },
    handelBtnClick4(e) {
        if (e.detail.id === '1') {
            //刷新进度
            this.handelRefreshAccidentInfo()
        }
    },



    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.data.orderId = options.id || '765622a5e2784098922d13b293210259'

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getInsuranceOrderDetail()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage: function() {

    // }
})
